﻿#include "calendar.h"
#include <QApplication>
#include <windows.h>

HHOOK mouseHook = NULL;
HWND trayClockHwnd = NULL;
Calendar* g_main = NULL;
LRESULT CALLBACK MouseProc( int nCode, WPARAM wParam, LPARAM lParam )
{
    if(nCode == HC_ACTION)
    {
        if(wParam == WM_LBUTTONDOWN)
        {
            POINT pt;
            GetCursorPos(&pt);
            HWND hdl_other = WindowFromPoint(pt);
            if(hdl_other == trayClockHwnd)
            {
                RECT rect;
                SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
                int width = rect.right - rect.left;
                int height = rect.bottom - rect.top;
                g_main->setVisible(!g_main->isVisible());
                g_main->move(width - g_main->width(), height - g_main->height());
                return true;
            }
        }
    }
    return CallNextHookEx(mouseHook,nCode,wParam,lParam);//否则，如果返回给下一个钩子子程处理
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    HWND hShellTrayWnd = FindWindow( TEXT("Shell_TrayWnd"), NULL);//任务栏
    HWND hTrayNotifyWnd = FindWindowEx(hShellTrayWnd, NULL, TEXT("TrayNotifyWnd"), NULL);//托盘区域
    trayClockHwnd = FindWindowEx(hTrayNotifyWnd, NULL, TEXT("TrayClockWClass"), NULL); //时钟区域

    mouseHook = ::SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, 0);

    g_main = new Calendar;
    g_main->hide();

    return a.exec();
}
